package com.xdap.common.dao;


import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.fsapproval.module.pojo.ApprovalInstanceLogs;
import com.xdap.fsapproval.pojo.ApprovalDefinitionConfig;
import com.xdap.api.init.DatasourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

/**
 * @BelongsProject: com.adisseo.service.approval.dao
 * @Author: liuyang
 * @CreateTime: 2022/9/5 17:09
 * @Description:
 * @Version: 1.0
 */
@Repository
@RequiredArgsConstructor
public class ApprovalDefinitionConfigDao {
    //    @Autowired
//    private MpaasQueryFactory sw;
    private final DatasourceService datasourceService;


    public ApprovalDefinitionConfig getFeishu(String formId) {
        MpaasQuery buildQuery = this.datasourceService.buildTenantMpaasQuery("xdap_manage_database");
        return buildQuery
                .eq("apaas_form_id", formId)
                .doQueryFirst(ApprovalDefinitionConfig.class);
    }

    public void insertapprovalDefinitionConfig(ApprovalDefinitionConfig approvalDefinitionConfig) {
        MpaasQuery buildQuery = this.datasourceService.buildTenantMpaasQuery("xdap_manage_database");

        buildQuery
                .doInsert(approvalDefinitionConfig);
    }

    public void insertApprovalInstanceLogs(ApprovalInstanceLogs approvalInstanceLogs) {
        MpaasQuery buildQuery = this.datasourceService.buildTenantMpaasQuery("xdap_manage_database");
        buildQuery
                .doInsert(approvalInstanceLogs);
    }

    public Integer updateApprovalInstanceLogs(ApprovalInstanceLogs approvalInstanceLogs) {
        MpaasQuery buildQuery = this.datasourceService.buildTenantMpaasQuery("xdap_manage_database");
        return buildQuery
                .eq("document_id", approvalInstanceLogs.getDocumentId())
                .eq("form_id", approvalInstanceLogs.getFormId())
                .doUpdate(approvalInstanceLogs);
    }

    public ApprovalInstanceLogs getApprovalInstanceLogs(String documentId) {
        MpaasQuery buildQuery = this.datasourceService.buildTenantMpaasQuery("xdap_manage_database");

        return buildQuery
                .eq("document_id", documentId)
                .doQueryFirst(ApprovalInstanceLogs.class);
    }
}
